parser grammar RuleParser;

options {
	tokenVocab = RuleLexer;
}

rules: (ruleDec)+;

// 一条规则
ruleDec: RULE DECIMAL_LIT? IDENTIFIER (DOT IDENTIFIER)* body;

body:
	L_CURLY (META typeExpression? urlExpression? descExpression?)? mainExpression R_CURLY;

typeExpression: TYPE ASSIGN STRING_LIT;
urlExpression: URL ASSIGN STRING_LIT;
descExpression: DESCRIPTION ASSIGN STRING_LIT;

mainExpression:
	STRINGS lineExpression+ CONDITION conditionExpression;

lineExpression: (assignExpression | printExpression);

assignExpression:
	(IDENTIFIER '=' |) (
		STRING_LIT
		| STRING_REGEXP_LIT
		| HEX_LIT
		| HEX_REGEXP_LIT
		| arithmeticExpression
		| zipSubFileExpression
		| apkManifestExpression
		| apkManifestActivityExpression
		| apkManifestReceiverExpression
		| apkManifestServiceExpression
		| apkManifestProviderExpression
		| apkManifestPermissionExpression
		| apkManifestActionExpression
		| APK_MANIFEST_MAIN_ACTIVITY
		| APK_MANIFEST_APPLICATION
		| apkDexStringExpression
		| apkDexClassExpression
		| apkSubFileExpression
	);

// 参数定义，支持字符串、正则表达式、16进制字符串
argumentExpression: (
		STRING_LIT
		| STRING_REGEXP_LIT
		| HEX_LIT
		| HEX_REGEXP_LIT
	);
// 1个参数
oneArgumentExpression: L_PAREN argumentExpression R_PAREN;
// 2个参数
twoArgumentExpression:
	L_PAREN argumentExpression COMMA argumentExpression R_PAREN;

apkManifestExpression: APK_MANIFEST oneArgumentExpression;
apkManifestActivityExpression:
	APK_MANIFEST_ACTIVITY oneArgumentExpression;
apkManifestReceiverExpression:
	APK_MANIFEST_RECEIVER oneArgumentExpression;
apkManifestServiceExpression:
	APK_MANIFEST_SERVICE oneArgumentExpression;
apkManifestProviderExpression:
	APK_MANIFEST_PROVIDER oneArgumentExpression;
apkManifestPermissionExpression:
	APK_MANIFEST_PERMISSION oneArgumentExpression;
apkManifestActionExpression:
	APK_MANIFEST_ACTION oneArgumentExpression;
apkManifestMainActivityExpression: APK_MANIFEST_MAIN_ACTIVITY;

apkDexStringExpression: APK_DEX_STRING oneArgumentExpression;
apkDexClassExpression:
	APK_DEX_CLASS L_PAREN (
		IDENTIFIER
		| STRING_LIT
		| STRING_REGEXP_LIT
	) R_PAREN;
apkDexMethodExpression: APK_DEX_METHOD oneArgumentExpression;
apkDexUsageExpression: APK_DEX_USAGE twoArgumentExpression;

apkSubFileExpression: APK_SUB_FILE twoArgumentExpression;

zipSubFileExpression: ZIP_SUB_FILE twoArgumentExpression;

printExpression: PRINT L_PAREN IDENTIFIER R_PAREN;

// 加减乘除
arithmeticOperator: ( PLUS | MINUS | STAR | DIV);

arithmeticExpression: // 算术语句，用于计算
	DECIMAL_LIT
	| FLOAT_LIT
	| IDENTIFIER
	| left = arithmeticExpression arithmeticOperator right = arithmeticExpression
	| '(' inner = arithmeticExpression ')';

ofExpression: (ANY | ALL | DECIMAL_LIT) OF (
		DOLLAR IDENTIFIER
		| THEM
	);

relationOperator: (
		GREATER
		| GREATER_OR_EQUALS
		| EQUALS
		| NOT_EQUALS
		| LESS
		| LESS_OR_EQUALS
	);
relationExpression:
	left = arithmeticExpression relationOperator right = arithmeticExpression;
// 逻辑表达式，结果为布尔值。
logicalOperator: (LOGICAL_OR | LOGICAL_AND);
notExpression: NOT logicalExpression;

conditionExpression: logicalExpression;

// 无限嵌套怎么解析？
logicalExpression:
	IDENTIFIER
	| ofExpression
	| relationExpression
	| L_PAREN inner = logicalExpression R_PAREN
	| left = logicalExpression logicalOperator right = logicalExpression
	| notExpression;
